package boot.spring.util;

import boot.spring.entity.Users;
import boot.spring.mapper.ClockMapper;
import boot.spring.mapper.UserInfoMapper;
import boot.spring.mapper.UsersMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/*用于每天凌晨1点把每位用户添加到打卡记录表*/
@Configuration      //1.主要用于标记配置类，兼备Component的效果。
@EnableScheduling   // 2.开启定时任务
public class Timingtask {
    /*获取当前年月日*/
    Date d = new Date();
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
    String dateNowStr = sdf.format(d);
    @Autowired
    private UserInfoMapper userInfoMapper;

    @Autowired/*注入用户持久层*/
    private UsersMapper usersMapper;

    @Autowired/*注入打卡记录表的持久层*/
    private ClockMapper clockMapper;
    /*每天1点把每个用户的id记录新增到打卡记录表里面
     *定时任务
     */
    //3.添加定时任务
    @Scheduled(cron = "0 0 1 * * ?")/*每天凌晨1点开始*/
    public void UserClockAdd(){
        /*获取到所有用户的id*/
        List<String> uid=usersMapper.userid();
        /*每天凌晨1点把每位用户添加到打卡记录表*/
        for(Integer i=0;i<uid.size();i++){
            Integer add=clockMapper.UserClockAdd(uid.get(i),dateNowStr);
        }
    }


    //每月根据用户的跟踪记录去修改当前用户的权重
    @Scheduled(cron = "0 0 1 * * ?")
    public void UserResultsUp(){
        /*获取到所有的用户*/
        List<Users> listuser=userInfoMapper.getAllUser();
        for(int i=0;i<listuser.size();i++){
            /*取出下标为 i 的一条数据*/
            Users user=listuser.get(i);
            /*判断 results 列的条数，
            *如果在0到10之间则权重为1
            *如果在10到30之间则权重为2，
            *如果在30到50之间则权重为3以此类推
            *并且把积累的跟踪条数清零
            * */
            if(user.getResults()>=0 &&user.getResults()<10){
                Integer uid=Integer.parseInt(user.getUid());
                Integer wei=usersMapper.UserWeightUp(0,1,uid);
            }else if(user.getResults()>=10 &&user.getResults()<30){
                Integer uid=Integer.parseInt(user.getUid());
                Integer wei=usersMapper.UserWeightUp(0,2,uid);
            }else if(user.getResults()>=30 &&user.getResults()<50){
                Integer uid=Integer.parseInt(user.getUid());
                Integer wei=usersMapper.UserWeightUp(0,3,uid);
            }else if(user.getResults()>=50 &&user.getResults()<80){
                Integer uid=Integer.parseInt(user.getUid());
                Integer wei=usersMapper.UserWeightUp(0,4,uid);
            }else if(user.getResults()>=80){
                Integer uid=Integer.parseInt(user.getUid());
                Integer wei=usersMapper.UserWeightUp(0,5,uid);
            }
        }
    }

}
